package com.whut.customer.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whut.common.util.R;
import com.whut.customer.entity.PlaneInformation;
import com.whut.customer.service.IPlaneInformationService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 * <p>
 * 飞机信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-02-24
 */
@RestController
@RequestMapping("/planeInformation")
public class PlaneInformationController {

    @Autowired
    IPlaneInformationService iPlaneInformationService;

    @ApiOperation("添加飞机信息")
    @PostMapping("/addPlane")
    public R addPlane(@RequestBody PlaneInformation plane){
//        if(StringUtil.isNull(plane.getPlaneId()) || StringUtil.isNull(plane.getPlaneName()))
//            return R.failed("飞机名不能为空");
        LambdaQueryWrapper<PlaneInformation> eq=new LambdaQueryWrapper<PlaneInformation>()
                .eq(PlaneInformation::getPlaneIndex,plane.getPlaneIndex())
                .eq(PlaneInformation::getPlaneId,plane.getPlaneId())
                .eq(PlaneInformation::getStatus,0);
        List<PlaneInformation> planeList=iPlaneInformationService.list(eq);
        if(!CollectionUtils.isEmpty(planeList))
                return R.failed("该飞机以及存在");
        return R.ok(iPlaneInformationService.save(plane));
    }

    @ApiOperation("修改飞机信息")
    @PostMapping("updatePlane")
    public R updatePlane(@RequestBody PlaneInformation plane){
        if(plane.getId()==null || plane.getPlaneId()==null)
                return R.failed("此飞机不存在");
        LambdaQueryWrapper<PlaneInformation> eq=new LambdaQueryWrapper<PlaneInformation>()
                .eq(PlaneInformation::getPlaneId,plane.getPlaneId())
                .eq(PlaneInformation::getPlaneIndex,plane.getPlaneIndex())
                .eq(PlaneInformation::getStatus,0);
        List<PlaneInformation> planeList=iPlaneInformationService.list(eq);
        if(!CollectionUtils.isEmpty(planeList))
            return R.failed("该飞机已存在");
        return R.ok(iPlaneInformationService.saveOrUpdate(plane));
    }

    @ApiOperation("通过id删除飞机信息")
    @PostMapping("/deletePlaneById")
    public R deletePlane(@PathVariable int id){
        return  R.ok(iPlaneInformationService.removeById(id));
    }

    @ApiOperation("通过planeId删除飞机信息")
    @PostMapping("/deletePlaneByPlaneId")
    public R delete(@PathVariable int planeId){
        if(planeId==0)
            return R.failed("此飞机不存在");
        LambdaQueryWrapper<PlaneInformation> eq=new LambdaQueryWrapper<PlaneInformation>()
                .eq(PlaneInformation::getPlaneId,planeId)
                .eq(PlaneInformation::getStatus,0);
        List<PlaneInformation> planeList=iPlaneInformationService.list(eq);
        if(CollectionUtils.isEmpty(planeList))
            return R.failed("该飞机不存在");
        return R.ok();
    }
    @ApiOperation("查找飞机信息")
    @PostMapping("/selectPlane")
    public R<List<PlaneInformation>> selectPlane(@RequestBody PlaneInformation plane) {
        LambdaQueryWrapper<PlaneInformation> eq = new LambdaQueryWrapper<PlaneInformation>()
                .eq(PlaneInformation::getPlaneId, plane.getPlaneId())
                .eq(PlaneInformation::getPlaneIndex, plane.getPlaneIndex())
                .eq(PlaneInformation::getStatus, 0);
        return R.ok(iPlaneInformationService.list(eq));
    }
}
